package com.itfreer.ui.base;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;

import com.itfreer.ui.R;
import com.itfreer.ui.standard.IDataView;
import com.itfreer.ui.standard.IPower;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 自定义CheckGroup
 */
public class CheckGroup extends LinearLayout implements IDataView, IPower {

    private LinearLayout linearLayout;

    private String fieldName;
    private Object bindData;
    private String securityID;
    private boolean securityVisible=true;

    /**
     * RadioButton缓存
     */
    private final Map<Object, CheckBox> checkBoxMap = new HashMap<Object, CheckBox>();

    public CheckGroup(Context context) {
        super(context);
        setView(context);
    }

    public CheckGroup(Context context, AttributeSet attrs){
        super(context, attrs);
        setView(context);
        getAttrs(context, attrs);
    }

    public CheckGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setView(context);
        getAttrs(context, attrs);
    }

    protected void getAttrs(Context context, AttributeSet attrs){
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SimpleView);
        this.fieldName = a.getString(R.styleable.SimpleView_fieldName);
        securityID = a.getString(R.styleable.SimpleView_securityID);
    }

    /**
     * 设置绑定的依据对象
     * @param name
     */
    @Override
    public void setFieldName(String name){
        this.fieldName = name;
    }

    /**
     * 获取绑定的依据对象
     * @return
     */
    @Override
    public String getFieldName(){
        return fieldName;
    }

    /**
     * 设置绑定的值
     * @param value
     */
    @Override
    public void setFieldValue(Object value){
        List<Object> valueList = null;
        if(value instanceof String){
            String values = (String)value;
            String[] valueArray = values.split(",");
            valueList = new ArrayList<Object>();
            for(String item : valueArray){
                valueList.add(item);
            }
        } else if(value instanceof List){
            valueList = (List)value;
        }

        if(valueList!=null){
            Set<Object> rKeys = checkBoxMap.keySet();
            for (Object rKey : rKeys) {
                if(valueList!=null && valueList.contains(rKey)){
                    checkBoxMap.get(rKey).setChecked(true);
                } else {
                    checkBoxMap.get(rKey).setChecked(false);
                }
            }
        }
    }

    /**
     * 获取当前的值
     * @return
     */
    @Override
    public Object getFieldValue(){
        List<Object> values = new ArrayList<Object>();
        Set<Object> rKeys = checkBoxMap.keySet();
        for (Object rKey : rKeys) {
            if(checkBoxMap.get(rKey).isChecked()){
                values.add(rKey);
            }
        }

        String result = "";
        for(Object item : values){
            result += item.toString() + ",";
        }
        if(result != ""){
            result = result.substring(0, result.length()-1);
        }
        return result;
    }

    /**
     * 设置绑定的数据
     * @param data
     */
    @Override
    public void setBindData(Object data){
        bindData = data;
        initRadios();
    }

    /**
     * 获取绑定的数据
     * @return
     */
    @Override
    public Object getBindData(){
        return bindData;
    }

    /**
     * 设置当前的视图
     * @param context
     */
    protected void setView(Context context){
        linearLayout = new LinearLayout(context);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.setGravity(Gravity.CENTER_VERTICAL);
        LayoutParams param =
                new LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);
        this.addView(linearLayout, param);
    }

    /**
     * 重新初始化控件
     */
    protected void initRadios(){
        linearLayout.removeAllViews();
        checkBoxMap.clear();

        if(bindData==null){
            return;
        }
        if(!(bindData instanceof Map)){
            return;
        }

        Map data = (Map)bindData;
        if(data.keySet().size()<=0){
            return;
        }

        LayoutParams param =
                new LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);
//        param.leftMargin = 5;
//        param.rightMargin = 5;
        param.gravity = Gravity.CENTER_VERTICAL;

        Set<Object> keySets = data.keySet();
        Integer index = 0;
        for(Object key : keySets){
            CheckBox checkBox = new CheckBox(this.getContext());
            linearLayout.addView(checkBox, param);
            checkBox.setText(data.get(key).toString());
            checkBox.setTag(key);
            checkBoxMap.put(key, checkBox);
            index++;
        }
    }

    /**
     * 获取权限ID
     * @return
     */
    public String getSecurityID(){
        return securityID;
    }

    /**
     * 没有权限时，设置是否可见
     * @param visible
     */
    public void setSecurityVisible(boolean visible){
        securityVisible = visible;
        if(visible){
            this.setVisibility(View.VISIBLE);
        }else{
            // 表示不可见并且不占用空间
            this.setVisibility(View.GONE);
        }
    }
}